import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/stores/userStore";
// createRouter 创建路由实例
// 配置history模式
// createWebHashHistory 地址栏不带#号
// createWebHistory
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/login", component: () => import("../views/login/Login.vue") },
    { path: "/admin", component: () => import("../views/admin/Admin.vue"), meta: { requiresAuth: true, isAdmin: true }, },
    {
      path: "/",
      component: () => import("../views/main/Main.vue"),
      redirect: "/main/home",
      children: [
        {
          path: "/main/home",
          name: "home",
          component: () => import("../views/home/Home.vue"),
        },
        {
          path: "/main/product/:id",
          name: "ProductDetail",
          component: () => import("../views/product/ProductDetail.vue"),
        },
        {
          path: "/main/cart",
          component: () => import("../views/cart/Cart.vue"),
          meta: { requiresAuth: true },
        },
        {
          path: "/main/favorite",
          component: () => import("../views/favorite/FavoriteList.vue"),
          meta: { requiresAuth: true },
        },
        {
          path: "/main/profile",
          component: () => import("@/views/profile/MainLayout.vue"),
          meta: { requiresAuth: true },
          children: [
            {
              path: "user-center",
              redirect: "user-center/orders",
              children: [
                {
                  path: "orders",
                  name: "订单管理",
                  component: () =>
                    import("@/views/profile/UserCenter/OrderManagement.vue"),
                  meta: { requiresAuth: true },
                },
              ],
            },
            {
              path: "account-center",
              redirect: "account-center/address",
              children: [
                {
                  path: "address",
                  name: "收货地址",
                  component: () =>
                    import("@/views/profile/AccountCenter/Address.vue"),
                  meta: { requiresAuth: true },
                },
                {
                  path: "settings",
                  name: "账号设置",
                  component: () =>
                    import("@/views/profile/AccountCenter/Setting.vue"),
                  meta: { requiresAuth: true },
                },
                {
                  path: "profile",
                  name: "个人信息",
                  component: () =>
                    import("@/views/profile/AccountCenter/Profile.vue"),
                  meta: { requiresAuth: true },
                },
                {
                  path: "ticket-buyer",
                  name: "常用购票人",
                  component: () =>
                    import("@/views/profile/AccountCenter/TicketBuyer.vue"),
                  meta: { requiresAuth: true },
                },
              ],
            },
            {
              path: "shop-center",
              redirect: "shop-center/manage",
              children: [
                {
                  path: "manage",
                  name: "商家订单管理",
                  component: () =>
                    import("@/views/profile/ShoppingCenter/ShoppingCenet.vue"),
                  meta: { requiresAuth: true },
                },
                {
                  path: "add",
                  name: "商品添加",
                  component: () =>
                    import("@/views/profile/ShoppingCenter/addProduct.vue"),
                  meta: { requiresAuth: true },
                },
              ],
            },
            // 新增消息中心路由
            {
              path: "message-center",
              name: "消息中心",
              component: () => import("@/views/profile/MessageCenter/MessageCenter.vue"),
              meta: { requiresAuth: true },
            },
            {
          path: "message-center/detail/:id",
          name: "MessageDetail",
          component: () => import("@/views/profile/MessageCenter/MessageDetail.vue"),
          meta: { requiresAuth: true },
        },
          ],
        },
        {
          path: "/main/order",
          name: "OrderManagement",
          component: () =>
            import("@/views/profile/UserCenter/OrderManagement.vue"),
          meta: { requiresAuth: true },
        },
      ],
    },
  ],
});

// 简化后的路由守卫

router.beforeEach((to, from, next) => {
  const userStore = useUserStore(); // 引入 Pinia Store
  
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next("/login");
  } else if (to.meta.isAdmin && userStore.getUserRole() !== "admin") {
    next("/main/home");
  } else {
    next();
  }
});

export default router;
